package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.SharedString;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysUser;
import com.powernode.model.Result;
import com.powernode.service.SysUserService;
import com.powernode.utils.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "管理员接口管理")
@RequestMapping("sys/user")
@RestController
public class SysUserController {
    @Autowired
    SysUserService sysUserService;

    /**
     * 加载当前登录的系统用户信息
     *
     * @return 返回包含系统用户信息的Result对象
     */
    @ApiOperation("查询得登录用户信息")
    @GetMapping("info")
    public Result<SysUser> loadSysUserInfo() {
        // 获取当前登录用户的ID
        Long userId = AuthUtils.getLoginUserId();
        // 根据用户ID查询用户信息
        SysUser sysUser = sysUserService.getById(userId);
        // 返回查询到的用户信息
        return Result.success(sysUser);
    }

    /**
     * 多条件分页查询管理员
     *
     * @param current  当前页码
     * @param size     每页大小
     * @param username 用户名（可选）
     * @return 分页查询结果
     */
    @ApiOperation("多条件分页查询管理员")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:user:page')")    // 表示该方法需要进行权限预授权
    public Result<Page<SysUser>> loadSysUserPage(@RequestParam Long current, @RequestParam Long size, @RequestParam(required = false) String username) {
        // 初始化分页对象
        Page<SysUser> page = new Page<>(current, size);
        // 多条件分页查询系统管理员
        // 如果用户名非空，则添加用户名模糊查询条件
        // 结果按创建时间降序排列
        page = sysUserService.page(page, new LambdaQueryWrapper<SysUser>().like(StringUtils.hasText(username), SysUser::getUsername, username).orderByDesc(SysUser::getCreateTime));
        // 返回查询结果
        return Result.success(page);
    }

    /**
     * 新增管理员
     */
    @ApiOperation("新增管理员")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:user:save')")
    public Result<String> saveSysUser(@RequestBody SysUser sysUser) {
        Integer count = sysUserService.saveSysUser(sysUser);
        return Result.handle(count > 0);
    }

    @ApiOperation("根据标识查询系统管理员信息")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public Result<SysUser> loadSysUserInfo(@PathVariable Long id) {
        SysUser sysUser = sysUserService.querySysUserInfoByUserId(id);
        return Result.success(sysUser);
    }

    @ApiOperation("修改管理员信息")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result<String> modifySysUserInfo(@RequestBody SysUser sysUser) {
        Integer count = sysUserService.modifySysUserInfo(sysUser);
        return Result.handle(count > 0);
    }

    @ApiOperation("单个或批量删除管理员")
    @DeleteMapping("{userIds}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<String> removeSysUsers(@PathVariable List<Long> userIds) {
        Boolean removed = sysUserService.removeSysUserListByUserIds(userIds);
        return Result.handle(removed);
    }
}